﻿using HKB.Common;
using HKB.Data;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public partial class Admin_Admin_ManageAd : Admin, IRequiresSessionState
{
    private string startstr = "//-----------------------开始";
    private string endstr = "//-----------------------结束";
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(12))
        {
            base.WriteLog("View", Language.GetAdminMsg("Log/Access", "0"), "Ads");
            base.WriteRightErrMsg();
        }
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        int i = 0;
        while (i < this.GridView1.Rows.Count)
        {
            float num = (float)int.Parse(this.GridView1.Rows[i].Cells[5].Text);
            float num2 = (float)int.Parse(this.GridView1.Rows[i].Cells[4].Text);
            if (num2 != 0f)
            {
                float num3 = num / num2 * 100f;
                this.GridView1.Rows[i].Cells[6].Text = num3.ToString() + "%";
            }
            else
            {
                this.GridView1.Rows[i].Cells[6].Text = "0%";
            }
            string text = this.GridView1.Rows[i].Cells[2].Text.Trim();
            if (text == null)
            {
                goto IL_278;
            }
            if (!(text == "1"))
            {
                if (!(text == "2"))
                {
                    if (!(text == "3"))
                    {
                        if (!(text == "4"))
                        {
                            if (!(text == "5"))
                            {
                                goto IL_278;
                            }
                            this.GridView1.Rows[i].Cells[2].Text = " 页面";
                        }
                        else
                        {
                            this.GridView1.Rows[i].Cells[2].Text = "代码";
                        }
                    }
                    else
                    {
                        this.GridView1.Rows[i].Cells[2].Text = " 文本";
                    }
                }
                else
                {
                    this.GridView1.Rows[i].Cells[2].Text = " 动画";
                }
            }
            else
            {
                HiddenField hiddenField = (HiddenField)this.GridView1.Rows[i].FindControl("H_Img");
                this.GridView1.Rows[i].Cells[2].Text = "<a href='javascript:void(0)' title='<img src=\"" + hiddenField.Value + "\"/>'> 图片</a>";
            }
            try
            {
            IL_2A4:
                DateTime t = DateTime.Parse(this.GridView1.Rows[i].Cells[8].Text);
                if (DateTime.Now.Date > t)
                {
                    this.GridView1.Rows[i].Cells[8].Text = t.ToShortDateString() + "已经关闭";
                    this.GridView1.Rows[i].Cells[8].ForeColor = Color.Red;
                }
                else
                {
                    this.GridView1.Rows[i].Cells[8].Text = t.ToShortDateString();
                }
            }
            catch
            {
            }
            this.GridView1.Rows[i].Cells[4].Text = this.GetZoneNameByZoneID(this.GridView1.Rows[i].Cells[4].Text);
            i++;
            continue;
        IL_278:
            this.GridView1.Rows[i].Cells[2].Text = "类型错误";
            //TODO 刘林注释，这部分代码需要看下原理，重新实现
            //goto IL_2A4;
        }
    }
    protected string GetZoneNameByZoneID(string ZoneIDArray)
    {
        string result;
        if (ZoneIDArray != null && ZoneIDArray != "")
        {
            AdZoneBll adZoneBll = new AdZoneBll();
            DataTable zoneByZoneID = adZoneBll.GetZoneByZoneID(ZoneIDArray);
            string text = "";
            if (zoneByZoneID != null && zoneByZoneID.Rows.Count > 0)
            {
                foreach (DataRow dataRow in zoneByZoneID.Rows)
                {
                    text += string.Format("<a href=\"Admin_UpdateAdzone.aspx?UpdateZoneID={0}\">{1}</a><br/>", dataRow["ZoneID"].ToString(), dataRow["ZoneName"].ToString());
                }
            }
            result = text;
        }
        else
        {
            result = "无";
        }
        return result;
    }
    protected void GridView1_SelectedIndexChanged(object sender, CommandEventArgs e)
    {
        if (!base.admin.CheckAdminPower(14))
        {
            MessageBox.Show(this, "对不起，你没有相应的权限,请跟系统超级管理员联系!");
        }
        else
        {
            int aDID = int.Parse(e.CommandArgument.ToString());
            AdBll adBll = new AdBll();
            DataRow dataRow = adBll.GetSingeleAd(aDID).Rows[0];
            string endTime = DateTime.Parse(dataRow["EndTime"].ToString()).ToShortDateString();
            string aDType = dataRow["ADType"].ToString();
            string aDName = dataRow["ADName"].ToString();
            string imgUrl = dataRow["ImgUrl"].ToString();
            string imgWidth = dataRow["ImgWidth"].ToString();
            string imgHeight = dataRow["ImgHeight"].ToString();
            string flashWmode = dataRow["FlashWmode"].ToString();
            string aDIntro = dataRow["ADIntro"].ToString();
            string linkUrl = dataRow["LinkUrl"].ToString();
            string linkTarget = dataRow["LinkTarget"].ToString();
            string linkAlt = dataRow["LinkAlt"].ToString();
            string priority = dataRow["Priority"].ToString();
            string text = dataRow["CountView"].ToString();
            string text2 = dataRow["CountClick"].ToString();
            string[] array = dataRow["ZoneID"].ToString().Split(new char[]
			{
				','
			});
            if (text2.ToLower() == "true")
            {
                text2 = "1";
            }
            else
            {
                text2 = "0";
            }
            if (text.ToLower() == "true")
            {
                text = "1";
            }
            else
            {
                text = "0";
            }
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != "")
                {
                    this.DelZoneJSRow(aDID.ToString(), aDType, aDName, imgUrl, imgWidth, imgHeight, flashWmode, aDIntro, linkUrl, linkTarget, linkAlt, priority, text, text2, array[i], endTime);
                }
            }
            adBll.DeleteAd(aDID);
            this.AdDatasource.Update();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(15))
        {
            MessageBox.Show(this, "对不起，你没有相应的权限,请跟系统超级管理员联系!");
        }
        else
        {
            string text = "";
            foreach (GridViewRow gridViewRow in this.GridView1.Rows)
            {
                CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckBox1");
                if (checkBox.Checked)
                {
                    int num = int.Parse(((HiddenField)gridViewRow.Cells[0].FindControl("hfId")).Value);
                    text = text + num + ",";
                    AdBll adBll = new AdBll();
                    DataRow dataRow = adBll.GetSingeleAd(num).Rows[0];
                    string endTime = DateTime.Parse(dataRow["EndTime"].ToString()).ToShortDateString();
                    string aDType = dataRow["ADType"].ToString();
                    string aDName = dataRow["ADName"].ToString();
                    string imgUrl = dataRow["ImgUrl"].ToString();
                    string imgWidth = dataRow["ImgWidth"].ToString();
                    string imgHeight = dataRow["ImgHeight"].ToString();
                    string flashWmode = dataRow["FlashWmode"].ToString();
                    string aDIntro = dataRow["ADIntro"].ToString();
                    string linkUrl = dataRow["LinkUrl"].ToString();
                    string linkTarget = dataRow["LinkTarget"].ToString();
                    string linkAlt = dataRow["LinkAlt"].ToString();
                    string priority = dataRow["Priority"].ToString();
                    string text2 = dataRow["CountView"].ToString();
                    string text3 = dataRow["CountClick"].ToString();
                    string[] array = dataRow["ZoneID"].ToString().Split(new char[]
					{
						','
					});
                    if (text3.ToLower() == "true")
                    {
                        text3 = "1";
                    }
                    else
                    {
                        text3 = "0";
                    }
                    if (text2.ToLower() == "true")
                    {
                        text2 = "1";
                    }
                    else
                    {
                        text2 = "0";
                    }
                    for (int i = 0; i < array.Length; i++)
                    {
                        if (array[i] != "")
                        {
                            this.DelZoneJSRow(num.ToString(), aDType, aDName, imgUrl, imgWidth, imgHeight, flashWmode, aDIntro, linkUrl, linkTarget, linkAlt, priority, text2, text3, array[i], endTime);
                            this.UpdateZoneJS(num.ToString(), aDType, aDName, imgUrl, imgWidth, imgHeight, flashWmode, aDIntro, linkUrl, linkTarget, linkAlt, priority, text2, text3, array[i], endTime);
                        }
                    }
                }
                MessageBox.Show(this, "审核成功！");
            }
            if (text != "")
            {
                string adiidarr = text.Remove(text.Length - 1);
                AdBll adBll = new AdBll();
                adBll.PassedAd(true, adiidarr);
            }
            this.AdDatasource.Update();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(15))
        {
            MessageBox.Show(this, "对不起，你没有相应的权限,请跟系统超级管理员联系!");
        }
        else
        {
            string text = "";
            foreach (GridViewRow gridViewRow in this.GridView1.Rows)
            {
                CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckBox1");
                if (checkBox.Checked)
                {
                    int num = int.Parse(((HiddenField)gridViewRow.Cells[0].FindControl("hfId")).Value);
                    text = text + num + ",";
                    AdBll adBll = new AdBll();
                    DataRow dataRow = adBll.GetSingeleAd(num).Rows[0];
                    string endTime = DateTime.Parse(dataRow["EndTime"].ToString()).ToShortDateString();
                    string aDType = dataRow["ADType"].ToString();
                    string aDName = dataRow["ADName"].ToString();
                    string imgUrl = dataRow["ImgUrl"].ToString();
                    string imgWidth = dataRow["ImgWidth"].ToString();
                    string imgHeight = dataRow["ImgHeight"].ToString();
                    string flashWmode = dataRow["FlashWmode"].ToString();
                    string aDIntro = dataRow["ADIntro"].ToString();
                    string linkUrl = dataRow["LinkUrl"].ToString();
                    string linkTarget = dataRow["LinkTarget"].ToString();
                    string linkAlt = dataRow["LinkAlt"].ToString();
                    string priority = dataRow["Priority"].ToString();
                    string text2 = dataRow["CountView"].ToString();
                    string text3 = dataRow["CountClick"].ToString();
                    string[] array = dataRow["ZoneID"].ToString().Split(new char[]
					{
						','
					});
                    if (text3.ToLower() == "true")
                    {
                        text3 = "1";
                    }
                    else
                    {
                        text3 = "0";
                    }
                    if (text2.ToLower() == "true")
                    {
                        text2 = "1";
                    }
                    else
                    {
                        text2 = "0";
                    }
                    for (int i = 0; i < array.Length; i++)
                    {
                        if (array[i] != "")
                        {
                            this.DelZoneJSRow(num.ToString(), aDType, aDName, imgUrl, imgWidth, imgHeight, flashWmode, aDIntro, linkUrl, linkTarget, linkAlt, priority, text2, text3, array[i], endTime);
                        }
                    }
                }
                MessageBox.Show(this, "反审核成功！");
            }
            if (text != "")
            {
                string adiidarr = text.Remove(text.Length - 1);
                AdBll adBll = new AdBll();
                adBll.PassedAd(false, adiidarr);
            }
            this.AdDatasource.Update();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(14))
        {
            MessageBox.Show(this, "对不起，你没有相应的权限,请跟系统超级管理员联系!");
        }
        else
        {
            string text = "";
            foreach (GridViewRow gridViewRow in this.GridView1.Rows)
            {
                CheckBox checkBox = (CheckBox)gridViewRow.FindControl("CheckBox1");
                if (checkBox.Checked)
                {
                    text = text + gridViewRow.Cells[1].Text + ",";
                    int aDID = int.Parse(gridViewRow.Cells[1].Text);
                    AdBll adBll = new AdBll();
                    DataRow dataRow = adBll.GetSingeleAd(aDID).Rows[0];
                    string endTime = DateTime.Parse(dataRow["EndTime"].ToString()).ToShortDateString();
                    string aDType = dataRow["ADType"].ToString();
                    string aDName = dataRow["ADName"].ToString();
                    string imgUrl = dataRow["ImgUrl"].ToString();
                    string imgWidth = dataRow["ImgWidth"].ToString();
                    string imgHeight = dataRow["ImgHeight"].ToString();
                    string flashWmode = dataRow["FlashWmode"].ToString();
                    string aDIntro = dataRow["ADIntro"].ToString();
                    string linkUrl = dataRow["LinkUrl"].ToString();
                    string linkTarget = dataRow["LinkTarget"].ToString();
                    string linkAlt = dataRow["LinkAlt"].ToString();
                    string priority = dataRow["Priority"].ToString();
                    string text2 = dataRow["CountView"].ToString();
                    string text3 = dataRow["CountClick"].ToString();
                    string[] array = dataRow["ZoneID"].ToString().Split(new char[]
					{
						','
					});
                    if (text3.ToLower() == "true")
                    {
                        text3 = "1";
                    }
                    else
                    {
                        text3 = "0";
                    }
                    if (text2.ToLower() == "true")
                    {
                        text2 = "1";
                    }
                    else
                    {
                        text2 = "0";
                    }
                    for (int i = 0; i < array.Length; i++)
                    {
                        if (array[i] != "")
                        {
                            this.DelZoneJSRow(aDID.ToString(), aDType, aDName, imgUrl, imgWidth, imgHeight, flashWmode, aDIntro, linkUrl, linkTarget, linkAlt, priority, text2, text3, array[i], endTime);
                        }
                    }
                }
            }
            if (text != "")
            {
                string arrayAdID = text.Remove(text.Length - 1);
                AdBll adBll = new AdBll();
                adBll.DeleteAds(arrayAdID);
            }
            this.AdDatasource.Update();
            MessageBox.Show(this, "审核成功");
        }
    }
    protected void UpdateZoneJS(string ADID, string ADType, string ADName, string ImgUrl, string ImgWidth, string ImgHeight, string FlashWmode, string ADIntro, string LinkUrl, string LinkTarget, string LinkAlt, string Priority, string CountView, string CountClick, string ZoneID, string EndTime)
    {
        string text = ZoneID.ToString();
        AdZoneBll adZoneBll = new AdZoneBll();
        string text2 = adZoneBll.GetSingeAdZone(int.Parse(ZoneID)).Rows[0]["ZoneJsName"].ToString();
        text2 = base.Server.MapPath("..") + "\\AD\\" + text2;
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        try
        {
            using (StreamReader streamReader = new StreamReader(text2, Encoding.Default))
            {
                string value;
                while ((value = streamReader.ReadLine()) != null)
                {
                    stringBuilder2.Append(value);
                    stringBuilder2.Append("\r\n");
                }
                streamReader.Close();
            }
        }
        catch (Exception ex)
        {
            ex.WriteErrorLog(new string[0]);
            MessageBox.Show(this, ex.Message);
        }
        try
        {
            stringBuilder2.Replace("ZoneAD_" + text + ".Show();", " ");
        }
        catch (Exception ex)
        {
            ex.WriteErrorLog(new string[0]);
            MessageBox.Show(this, ex.Message);
        }
        try
        {
            string text3 = "";
            if (HttpContext.Current.Request.Url.Port.ToString() != "80")
            {
                text3 = ":" + HttpContext.Current.Request.Url.Port.ToString();
            }
            string str = this.startstr + ADName.Trim() + "\n";
            string str2 = this.endstr + ADName.Trim() + "\n";
            string str3 = string.Concat(new string[]
			{
				" var objAD = new ObjectAD();\r\nobjAD.ADID           =",
				ADID.ToString().Trim(),
				";\r\nobjAD.ADType         =",
				ADType.ToString().Trim(),
				";\r\nobjAD.ADName         = \"",
				ADName.Trim(),
				"\";\r\nobjAD.ImgUrl         = \"",
				ImgUrl.Trim(),
				"\";\r\nobjAD.ImgWidth       = ",
				ImgWidth.ToString().Trim(),
				";\r\nobjAD.ImgHeight      = ",
				ImgHeight.ToString().Trim(),
				";\r\nobjAD.FlashWmode     = ",
				FlashWmode.ToString().Trim(),
				";\r\nobjAD.ADIntro        = \"",
				ADIntro.Trim(),
				"\";\r\nobjAD.LinkUrl        = \"",
				LinkUrl.ToString().Trim(),
				"\";\r\nobjAD.LinkTarget     = ",
				LinkTarget.ToString().Trim(),
				";\r\nobjAD.LinkAlt        = \"",
				LinkAlt.ToString().Trim(),
				"\";\r\nobjAD.Priority       = ",
				Priority.ToString().ToString(),
				";\r\nobjAD.CountView      = ",
				CountView.ToString().Trim(),
				";\r\nobjAD.CountClick     = ",
				CountClick.ToString().Trim(),
				";\r\nobjAD.EndTime =\"",
				EndTime.ToString().Trim(),
				"\";\r\nobjAD.InstallDir     =\"http://",
				HttpContext.Current.Request.Url.Host.ToString(),
				text3,
				"/\";\r\nobjAD.ADDIR          =\"AD\"; \r\nZoneAD_",
				text.ToString().Trim(),
				".AddAD(objAD);\r\n"
			});
            stringBuilder2.Insert(stringBuilder2.Length, str + str3 + str2);
        }
        catch (Exception ex)
        {
            ex.WriteErrorLog(new string[0]);
            MessageBox.Show(this, ex.Message);
        }
        stringBuilder2.Insert(stringBuilder2.Length, "ZoneAD_" + text + ".Show();\r\n");
        try
        {
            using (StreamWriter streamWriter = new StreamWriter(text2, false, Encoding.GetEncoding("GB2312")))
            {
                streamWriter.WriteLine(stringBuilder2);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }
        catch (Exception ex)
        {
            ex.WriteErrorLog(new string[0]);
            MessageBox.Show(this, ex.Message);
        }
    }
    private void DelZoneJSRow(string ADID, string ADType, string ADName, string ImgUrl, string ImgWidth, string ImgHeight, string FlashWmode, string ADIntro, string LinkUrl, string LinkTarget, string LinkAlt, string Priority, string CountView, string CountClick, string ZoneID, string EndTime)
    {
        string text = ZoneID.ToString();
        AdZoneBll adZoneBll = new AdZoneBll();
        string text2 = "";
        try
        {
            text2 = adZoneBll.GetSingeAdZone(int.Parse(ZoneID)).Rows[0]["ZoneJsName"].ToString();
        }
        catch
        {
            return;
        }
        text2 = base.Server.MapPath("..") + "\\AD\\" + text2;
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        try
        {
            using (StreamReader streamReader = new StreamReader(text2, Encoding.Default))
            {
                string str;
                while ((str = streamReader.ReadLine()) != null)
                {
                    stringBuilder2.Append(str + "\r\n");
                }
                streamReader.Close();
            }
        }
        catch (Exception ex)
        {
            ex.WriteErrorLog(new string[0]);
            MessageBox.Show(this, ex.Message);
        }
        try
        {
            string value = this.startstr + ADName.Trim();
            string text3 = this.endstr + ADName.Trim();
            string text4 = stringBuilder2.ToString();
            int num = text4.IndexOf(value);
            if (num > 0)
            {
                string text5 = text4.Substring(num, text4.IndexOf(text3) - num + text3.Length);
                if (text4.Contains(text5))
                {
                    text4 = text4.Replace(text5, "");
                }
            }
            stringBuilder.Append(text4);
        }
        catch (Exception ex)
        {
            ex.WriteErrorLog(new string[0]);
            MessageBox.Show(this, ex.Message);
        }
        try
        {
            using (StreamWriter streamWriter = new StreamWriter(text2, false, Encoding.GetEncoding("GB2312")))
            {
                streamWriter.WriteLine(stringBuilder);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }
        catch (Exception ex)
        {
            ex.WriteErrorLog(new string[0]);
            MessageBox.Show(this, "err.Message");
        }
    }
}
